View Javadoc

1   /*
2    * Copyright (c) 2004-2005, University Health Network.  All rights reserved. Distributed under the BSD 
3    * license (see http://opensource.org/licenses/bsd-license.php).
4    *  
5    * Created on 5-Jan-2005
6    */
7   package ca.uhn.cache.internal.impl;
8   
9   import java.util.Iterator;
10  import java.util.Map;
11  
12  import ca.uhn.cache.VolatilityEnum;
13  import ca.uhn.cache.internal.IChunk;
14  import ca.uhn.cache.internal.IStaleChunkRule;
15  
16  /***
17   * Default implementation of IStaleChunkRule. 
18   * 
19   * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
20   * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:54:05 $ by $Author: bryan_tripp $
21   */
22  public class DefaultStaleChunkRule implements IStaleChunkRule {
23  
24      private Map myVolatilityMap;
25      private Map myUpdateAgeMap;
26      private long myDefaultMaxAgeMillis;    
27      
28      /***
29       * @param theVolatilityMap keys are VolatilityEnum, values are corresponding max ages since 
30       *      cache time (type Long)
31       * @param theUpdateAgeMap keys are Long representing age since last update time, values are 
32       *      corresponding max ages since cache time (type Long)
33       * @param theDefaultMaxAgeMillis age at which chunk becomes stale by default, that is if it doesn't 
34       *      before stale sooner because of high volatility or recent last update  
35       */
36      public DefaultStaleChunkRule(Map theVolatilityMap, Map theUpdateAgeMap, long theDefaultMaxAgeMillis) {
37          myVolatilityMap = theVolatilityMap;
38          myUpdateAgeMap = theUpdateAgeMap;
39          myDefaultMaxAgeMillis = theDefaultMaxAgeMillis;
40      }
41  
42      /*** 
43       * @see ca.uhn.cache.internal.IStaleChunkRule#getMaxAge(ca.uhn.cache.internal.IChunk)
44       */
45      public long getMaxAge(IChunk theChunk) {
46          long maxAgeByVolatility = getMaxAge(theChunk.getVolatility());
47          long maxAgeByUpdateAge = getMaxAge(System.currentTimeMillis() - theChunk.getLastUpdateTime().getTime());        
48          return Math.min(maxAgeByVolatility, maxAgeByUpdateAge);
49      }
50  
51      /***
52       * @param theVolatility a volatility code  
53       * @return max age: if a chunk of this volatility was cached longer ago than this, it is stale 
54       */
55      private long getMaxAge(VolatilityEnum theVolatility) {
56          Long result = (Long) myVolatilityMap.get(theVolatility);
57          
58          if (result == null) {
59              return myDefaultMaxAgeMillis;
60          } else {
61              return result.longValue(); 
62          }
63      }
64      
65      /***
66       * @param theUpdateAge an age since last update 
67       * @return max age: if a chunk of this update age (or less) was cached longer than this, it is
68       *      stale
69       */
70      private long getMaxAge(long theUpdateAge) {
71          Long result = (Long) myUpdateAgeMap.get(new Long(theUpdateAge));
72          
73          if (result == null) {
74              return interpolateStaleAge(theUpdateAge);
75          } else {
76              return result.longValue();
77          }
78      }
79      
80      //for update times not exactly on boundaries 
81      private long interpolateStaleAge(long theUpdateAge) {
82          long result = myDefaultMaxAgeMillis;
83          long now = System.currentTimeMillis();
84          
85          for (Iterator it = myUpdateAgeMap.keySet().iterator(); it.hasNext(); ) {
86              Long updateAge = (Long) it.next();
87              long boundary = updateAge.longValue();
88  
89              //applies if updated since boundary time
90              if (theUpdateAge <= boundary) {
91                  long staleAge = ((Long) myUpdateAgeMap.get(updateAge)).longValue();
92                  if (staleAge < result) {
93                      result = staleAge;
94                  }
95              }
96          }
97          
98          return result;        
99      }
100     
101 }